package cn.paplink.deviceinfo;

import android.app.Application;
import android.content.Context;
import android.graphics.Point;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.os.Build;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

/**
 *
 * @author hdz
 * @date 2018/12/29
 */

public class MyApplication extends Application {
    private static MyApplication app;

    public static String mDevDisplaySize = "";
    public static String mDevDisplaySize2 = "";
    public static String mAppDisplaySize = "";
    public static String mAppDisplaySize2 = "";

    public static String mManufacturer = "";
    public static String mPlatform = "";
    public static String mModel = "";

    public static String mAndroidVer = "";
    public static String mDecoderName = "";

    public static MyApplication getApp() {
        if (app == null) {
            synchronized (MyApplication.class) {
                if (app == null) {
                    app = new MyApplication();
                }
            }
        }
        return app;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("DeviceInfo", "=========================================");
        long time1 = SystemClock.uptimeMillis();

        DisplayMetrics metric = new DisplayMetrics();
        WindowManager mWindowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        mWindowManager.getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;
        int height = metric.heightPixels;
        mAppDisplaySize = ""+width+"x"+height;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            mWindowManager.getDefaultDisplay().getRealMetrics(metric);
            width = metric.widthPixels;
            height = metric.heightPixels;
            mDevDisplaySize = ""+width+"x"+height;
        }
        Point point = Util.getResolution();
        mDevDisplaySize2 = point.x+"x"+point.y;
        Point appSize = Util.getAppDisplaySize();
        mAppDisplaySize2 = appSize.x+"x"+appSize.y;
        Log.e("DeviceInfo", "====获取分辨率消耗的时间："+(SystemClock.uptimeMillis()-time1));




        time1 = SystemClock.uptimeMillis();
        mManufacturer = Util.getSystemProperty("ro.product.manufacturer", "");
        mPlatform     = Util.getSystemProperty("ro.board.platform", "");
        mModel = Util.getSystemProperty("ro.product.model", "");

        //mPlatform     = Util.getSystemProperty("ro.product.name", "");
        mAndroidVer = Util.getSystemProperty("ro.build.version.release","") + "(" + Util.getSystemProperty("ro.build.version.sdk","") + ")";
        Log.i("DeviceInfo", "====hasNavigationBar: "+Util.hasNavigationBar());
        Log.e("DeviceInfo", "====获取设备信息消耗的时间："+(SystemClock.uptimeMillis()-time1));




        time1 = SystemClock.uptimeMillis();
        checkHwDecoder();
        Log.e("DeviceInfo", "====检查硬解解码器消耗的时间："+(SystemClock.uptimeMillis()-time1));
    }


    public static void checkHwDecoder() {
        try {
            int num = MediaCodecList.getCodecCount();
            for (int i = 0; i < num; i++) {
                MediaCodecInfo info = MediaCodecList.getCodecInfoAt(i);
                if (info.isEncoder()) {
                    continue;
                }
                String[] types = info.getSupportedTypes();
                for (String type : types) {
                    if (type.equals("video/avc")) {
                        if ("".equals(mDecoderName)) {
                            mDecoderName = info.getName();
                        } else {
                            mDecoderName = mDecoderName + "\n" + info.getName();
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
